/******************************************************
 *                                                     
 *              Score System               
 *                                                     
 *******************************************************/

/**
 * AddusrServlet.java
 *
 * 网页的响应Servlet
 *
 * @author sundy
 * @version 0.0.1
 * lastModified 2011-01-01
 */

package com.sundy.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sundy.core.Common;
import com.sundy.core.Context;
import com.sundy.core.Utility;
import com.sundy.daosupport.TeacherTblDAO;
import com.sundy.service.TeacherTbl;

public class AddusrServlet extends AbstractServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4660934698120918907L;

	public AddusrServlet() {
	}

	protected void doGet(Context context, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(context, request, response);
	}

	protected void doPost(Context context, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		if (name == null || name.trim().equals("") || password == null
				|| password.equals("")) {
			Utility.showError(request, response, "参数错误！");
			return;
		}
		TeacherTblDAO dao = TeacherTblDAO
				.getFromApplicationContext(Common.applicationContext);
		List<TeacherTbl> lst = dao.findByName(name);
		if (lst == null || lst.isEmpty()) {
			TeacherTbl user = new TeacherTbl(name, password, 0);
			dao.save(user);
			dao.getHibernateTemplate().flush();
			response.sendRedirect(request.getContextPath());
		} else {
			Utility.showError(request, response, "您添加到用户名已经存在！");
		}
	}
}
